﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.SharePoint.ApplicationPages;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Administration.AccessControl;
using Microsoft.SharePoint.Administration;

namespace SPaDevToolkit.Administration.UI
{
    public class SPaWebServiceApplicationAdminPage<SA, S> : GlobalAdminPageBase
        where SA : SPIisWebServiceApplication   // SPaCslaWebServiceApplication
        where S : SPIisWebService
    {
        private SA _ServiceApplication;

        protected Guid ServiceApplicationID
        {
            get { return (Request.QueryString.Count == 0 || Request.QueryString["ID"] == null) ? Guid.Empty : new Guid(Request.QueryString["ID"].ToString()); }
        }

        protected override bool AccessibleByDelegatedAdminGroup
        {
            get { return HasUserReadPermissionForCurrentPage; }
        }

        protected SA ServiceApplication
        {
            get
            {
                if (null == this._ServiceApplication)
                {
                    S service = SPFarm.Local.Services.GetValue<S>();

                    if (service == null)
                        throw new InvalidOperationException(string.Format("Service with type {0} is not installed", typeof(S).ToString()));

                    this._ServiceApplication = service.Applications.GetValue<SA>(ServiceApplicationID);
                }
                return this._ServiceApplication;
            }
        }

        protected bool HasUserReadPermissionForCurrentPage
        {
            get
            {
                if (ServiceApplication == null)
                    return false;

                if (!SPFarm.Local.CurrentUserIsAdministrator())
                {
                    if ((!ServiceApplication.CheckAdministrationAccess(SPCentralAdministrationRights.Read) && !ServiceApplication.CheckAdministrationAccess(SPCentralAdministrationRights.Write)) && (!ServiceApplication.CheckAdministrationAccess(SPCentralAdministrationRights.None | SPCentralAdministrationRights.ChangePermissions) && !ServiceApplication.CheckAdministrationAccess(~SPCentralAdministrationRights.None)))
                        return false;
                }

                return true;
            }
        }

        public enum PageEditMode
        {
            Create,
            Edit
        }

        protected DialogMaster DialogMaster
        {
            get { return (DialogMaster)Page.Master; }
        }

        protected PageEditMode PageMode
        {
            get
            {
                if (ServiceApplicationID == Guid.Empty)
                {
                    return PageEditMode.Create;
                }
                return PageEditMode.Edit;
            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            DialogMaster.OkButton.Click += OnOkButtonClick;
        }

        public void OnOkButtonClick(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                ((DialogMaster)this.Page.Master).OkButton.Visible = false;
                if (this.PageMode == PageEditMode.Create)
                {
                    this.OnCreateApplication();
                }
                else
                {
                    this.OnUpdateApplication();
                }
            }
        }

        protected virtual void OnCreateApplication()
        {
        }

        protected virtual void OnUpdateApplication()
        {
        }

    }
}
